package com.bms.cfcmms.dao;

import com.bms.cfcmms.model.system.module.Module;
import com.bms.cfcmms.model.viewsetting.Menu;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by carl on 2016/5/12.
 */
@Repository
public class MenuDao extends BaseDao<Menu> {

    private static final Logger log = LoggerFactory.getLogger(MenuDao.class);

    /**
     *通过指定属性查询所有菜单.
     *
     * @param module 菜单对应的模块信息
     * @param sort    排序的属性
     * @param dir     排序方式
     * @param propertyName  属性名
     * @param value    属性的值
     * @return   所有的菜单
     */
    public List<Menu> findMenuByPropertyAllSort(Module module, String sort, String dir,
                                                String propertyName, Object value) {
        log.debug("find all:" + module.getModuleName() + "---sort:" + sort + "--" + dir);

        String tableName = findRealTableName(module);
        String queryString = "select * from " + tableName + " as model where model."
                + propertyName + (value != null ? "=:" + propertyName : " is null ");
        if (sort != null && sort.length() > 0) {
            queryString += " order by " + sort + " " + dir;
        }

        SqlParameterSource sqlParameterSource = new MapSqlParameterSource(propertyName, value);
        return namedTemplate.query(queryString, sqlParameterSource, (RowMapper<Menu>) new Menu());
    }
}
